define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/preset/models'),
        utils = require('js/utils/tools'),
        uploader = require('js/utils/uploader'),
        opModelCls = require('js/apps/op/models'),
        moment = require('moment'),
        app = Backbone.history;
    require('multiselect');
    require('datetimepicker');

    var LotteryListView = Backbone.View.extend({
        tagName: "div",
        events: {
            'click a.onClickDelete': 'toDelete',
        },

        initialize: function (options) {
            this.options = options;
            this.collection = new models.LotteryCollection();
            this.collection.bind('change reset remove', this.renderWithData, this);
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        toDelete: function (e) {
            utils.deleteItem(e, this.collection);
        },
        renderWithData: function () {
            this.$el.html(this.template({
                lotterys: this.collection.toJSON(),
                searched: utils.getSearched('preset/lottery')
            }));
            var that = this;
            utils.renderTable('main-list', {
                $orderby: that.options.$orderby || 'id',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    that.options.$page = 1;
                    var newUrl = utils.composeQueryString('#preset/lottery/', that.options);
                    app.navigate(newUrl, {
                        trigger: true
                    });
                }
            });
            utils.getPaginator(this.options, this.collection.total, '#preset/lottery/');
            return this;
        },
        load: function () {
            this.collection.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                    $('#content').append('<h4 class="text-muted">无数据</h4>');
                },
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "preset/LotteryList.html",
        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            var view = new LotteryListView(this.options);
            view.template = this.template;

            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();

            return this;
        }
    });
    var LotteryDetailView = Backbone.View.extend({
        tagName: "div",
        className: "panel-body pn",
        events: {
            'click #save': 'toSaveOrCreate',
            'click #activeBtn': 'doSwitch'
        },
        initialize: function (options) {
            this.tabs = new models.TabCollection();
            this.model = new models.Lottery();
            if (options.id) {
                this.model.set('id', options.id);
            }
            this.model.bind('change reset', this.renderWithData, this);
            this.tabs.bind('reset', this.renderWithData, this);
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        toSaveOrCreate: function (e) {
            e.preventDefault();
            var attrs = utils.getAllInput('#content');
            if(!attrs.start_stop_lottery) attrs.start_stop_lottery = null;
            if(!attrs.end_stop_lottery) attrs.end_stop_lottery = null;
            attrs.device_type = utils.calcDeviceType($('#inputDeviceType').val());
            this.model.save(attrs, {
                patch: true,
                success: function (model, response) {
                    notify.success('提示', '保存成功！');
                    setTimeout(function () {
                        history.back();
                    }, 500);
                },
                error: function (model, response) {
                    notify.notifyResp(response);
                }
            });
            return false;
        },
        doSwitch: function () {
            notify.warning('警告', '设置中，请等待...');
            if ($('#activeBtn').val() == '已开启') {
                var off = 1;
            } else {
                var off = 0;
            }
            var lottery_type = this.model.toJSON().type;
            $.post('/admin/preset/lottery/'+lottery_type+'/switch/'+off+'/').done(function (resp) {
                var data = resp.data;
                if (off == 1) { 
                    $('#activeBtn').val('已关闭');
                    $('#activeBtn').text('已关闭');
                } else {
                    $('#activeBtn').val('已开启');
                    $('#activeBtn').text('已开启');
                }
                notify.success('提示', '修改成功！');
            }).fail(function (data) {
                notify.notifyResp(data);
            });
        },
        renderWithData: function () {
            this.$el.html(this.template({
                info: this.model.toJSON(),
                tabs: this.tabs.toJSON()
            }));
            utils.multiselect('.multiselect');
            $('.date-box').datetimepicker({
                format: 'YYYY-MM-DD'
            });
            $('.date-hour-box').datetimepicker({
                format: "YYYY-MM-DD HH:mm",
            });
            $('.image-popup').magnificPopup({
                type: 'image',
                closeOnContentClick: true,
                mainClass: 'mfp-img-mobile',
                image: {
                    verticalFit: true,
                    titleSrc: function (item) {
                        return '<a href="' + item.el.attr('href') +
                            '" target="_blank">查看原图</a>';
                    }
                }
            });
            uploader.getSingleUploader('uploadImage');
        },
        load: function () {
            this.tabs.fetch({
                    reset: true
                });
            if (this.model.id) {
                this.model.fetch({
                    reset: true
                });
            } else {
                this.renderWithData();
            }
        }
    });
    exports.Detail = Backbone.View.extend({
        el: "#content_wrapper",
        template: "preset/LotteryDetail.html",
        initialize: function (options) {
            this.options = options;
        },
        events: {
            "change #selectLottery": "changeSelectLottery"
        },

        changeSelectLottery: function(e){
            var lottery = e.currentTarget.value;
            var bet_type = window.BET_TYPE[lottery];
            var betTypeSelect = $('#betType');
            betTypeSelect.find('option').remove();
            var k;
            betTypeSelect.append('<option value="0">无</option>');
            for(k in bet_type){
                betTypeSelect.append('<option value='+ k +'>'+bet_type[k]+'</option>');}
        },
        render: function () {
            var view = new LotteryDetailView(this.options);
            view.template = this.template;

            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();

            return this;
        }
    });

});
